package com.vpen.springsecuritytest.mapper;

import com.vpen.springsecuritytest.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * -@Version 1.0
 * -@Author:Vpen
 * -@Date:2021/3/13 19:28
 * -@Content:查询数据
 */

@Mapper
@Repository(value = "userMapper") // 自定bean名字userMapper
public interface UserMapper {
    /**
     * 作者:Vpen
     * 描述:只根据用户名来查询用户，密码验证在内存中处理
     * @Param("xxx") String name---> #{xxx}
     * @Param("xxx") Map name---> #{xxx.key}
     * @Param("xxx") Object name---> #{Object.property}
     * */
    @Select("select * from user where user_name=#{name}")
    @Results(id = "selectUserByUserName",value = {
            @Result(column = "user_id",property = "id"),
            @Result(column = "user_name",property = "name"),
            @Result(column = "user_pwd",property = "pwd"),
            @Result(column = "user_login_enable",property = "loginEnable"),
            @Result(column = "user_login_error",property = "loginError"),
            @Result(column = "user_login_last_time",property = "loginLastTime"),
            @Result(column = "user_login_display_time",property = "displayName"),
            @Result(column = "user_login_email",property = "email"),
    })
    User selectUserByUserName(@Param("name") String name);

    // 根据用户id查询角色名称
    @Select("select role_name from role where id " +
            "in (select role_id from user_role where user_role.user_id=#{id})")
    List<String> selectRoleByUserId(Integer id);
    // 根据用户id查询角色资源
    @Select("select res from resource where id " +
            "in (select res_id from role_resource where role_id " +
            "in(select role_id from user_role where user_id=#{id}))")
    List<String> selectResourceByUserId(Integer id);

}
